/**
 * 
 */
package com.ai.notices.web.controller.busi;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.ai.notices.dao.mysql.model.AuthStaff;
import com.ai.notices.web.FileUtils;
import com.ai.notices.web.system.AppBaseController;
import com.ai.notices.web.system.util.StaffSessionUtil;
import com.alibaba.fastjson.JSONObject;

/**
 * @author chenjf6 号码管理
 */
@Controller
@RequestMapping(value = "/sendeeInfo")
public class SendeeInfoController extends AppBaseController {
	
	@SuppressWarnings({ "unused", "resource", "unchecked" })
	@RequestMapping(value = { "/ExcelImportProcess" }, method = { RequestMethod.POST })
	@ResponseBody
	public JSONObject ExcelImportProcess(@RequestParam("groupNum")
	MultipartFile mfile, Model model, HttpServletRequest request,
	HttpServletResponse response) throws Exception {
		String groupId = request.getParameter("groupId");
		JSONObject ret = new JSONObject();
		if (mfile == null) {
			ret.put("flag", false);
			ret.put("msg", "文件不能为空");
			return ret;
		}
		Workbook workBook = null;
		String strFileNmae = mfile.getOriginalFilename();
		if (FileUtils.isExcel2003(strFileNmae)) {
			workBook = new HSSFWorkbook(mfile.getInputStream());
		} else if (FileUtils.isExcel2007(strFileNmae)) {
			workBook = new XSSFWorkbook(mfile.getInputStream());
		} else {
			ret.put("flag", false);
			ret.put("msg", "导入文件不支持！");
			return ret;
		}
		AuthStaff staff = StaffSessionUtil.getStaffInfo(request.getSession());
		int nSheetNumber = workBook.getNumberOfSheets();
		Sheet sheet = null;
		nSheetNumber = 1;// 暂时只读取第一页
		for (int i = 0; i < nSheetNumber; i++) {
			sheet = workBook.getSheetAt(i);
			for (int rowIdx = sheet.getFirstRowNum(); rowIdx <= sheet
					.getLastRowNum(); rowIdx++) {
				Row row = sheet.getRow(rowIdx);
				if (sheet.getLastRowNum() < 1) {
					break;
				} else if (0 == row.getRowNum()) {
					// 如果为第一行标题栏，则跳过
					continue;
				}
				//SendeeInfo info = new SendeeInfo();
				String name = FileUtils.getStringVal(row.getCell(0));
				String mobile = FileUtils.getStringVal(row.getCell(1));
				String remark = FileUtils.getStringVal(row.getCell(2));
				if(checkLength(mobile,16)){
					//info.setPhoneNume(mobile);
				}
				if(checkLength(name,16)){
					//info.setSendeeName(name);
				}
				if(checkLength(name,50)){
					//info.setRemark(remark);
				}
				//info.setCreateStaff(staff.getId());
				//info.setCreateTime(new Timestamp(System.currentTimeMillis()));
				//list.add(info);
			}
		}
        ret.put("flag", true);
		ret.put("msg", "success");
		return ret;
	}
	private boolean checkLength(String value,int length){
		if(StringUtils.isNotEmpty(value)&&value.length()<length){
			return true;
		}
		return false;
	}
}
